<?php
/**
 * Created by PhpStorm.
 * User: janke
 * Date: 17-3-24
 * Time: 上午9:15
 */

namespace app\controllers;

use app\models\Comment;
use app\models\User;
use yii\rest\ActiveController;
use yii\filters\auth\QueryParamAuth;
use yii\filters\auth\CompositeAuth;
use app\models\Message;
use yii\web\Response;


class CommentController extends ActiveController
{

    public $modelClass = 'app\models\Comment';
    public $serializer = [
        'class' => 'yii\rest\Serializer',
    ];

    public function beforeAction($action)
    {
        User::$accessToken = \Yii::$app->request->get()['access-token'];
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function behaviors()
    {
        // set response type
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        // authorization
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
//                 HttpBearerAuth::className(),
                QueryParamAuth::className(),
            ]
        ];
        return $behaviors;
    }

    // 默认动作
    public function actions()
    {
        $actions = parent::actions(); // TODO: Change the autogenerated stub
        unset($actions['create'], $actions['delete'], $actions['update'], $actions['options']);
        return $actions;
    }

    /**
     * 发送评论
     * @POST messageID
     * @POST commentContent
     * @return array|static
     * @author Janke
     */
    public function actionInsert()
    {
        $userDataArray = User::findUserDataByAccessToken(User::$accessToken);
        $userName = $userDataArray['userName'];
        $postData = \Yii::$app->request->post();
        $postData['commentSender'] = $userName;
        $commentModel = new Comment();
        $commentModel->setAttributes($postData);
        if ($commentModel->validate() && $commentModel->insertComment())
        {
           return [
               'isSuccess' => 1
           ];
        }
        return $commentModel->errors;
    }

    /**
     * 根据commentId删除一条comment
     * 更新message_count
     * @return array
     * @author Janke
     */
    public function actionDelete_comment()
    {
        $postData = \Yii::$app->request->post();
        $commentModel = new Comment();
        if ($postData['commentId'])
        {
            $messageId = Comment::findByCommentId($postData['commentId'])->attributes['message_id'];
            if ($commentModel->deleteCommentById($postData['commentId'], $commentModel))
            {
                Message::downDateCommentCount($messageId);
                return [
                    'isSuccess' => '1'
                ];
            }
        }
        else
        {
            $commentModel->addError("error","commentId can not be blank");
        }
        return $commentModel->errors;
    }

    public function actionApprove()
    {

    }

    public function actionDisApprove()
    {

    }

    /**
     * message详情页面
     * @author janke
     * @return mixed Array
     */
    public function actionSearch()
    {
        $getData = \Yii::$app->request->get();
        $messageId = $getData['messageId'];
        $messageArray = Message::getMessageById($messageId);
        $commentModel = new Comment();
        // 载入参数 / 验证数据
        if ($commentModel->setAttributes($getData) && $commentModel->validate());
        {
            $commentsArray = $commentModel::findByMessageId($commentModel->messageId);
        }
        $returnArray['message'] = $messageArray;
        $returnArray['comments'] = $commentsArray;
        return $returnArray;
    }


}